package com.oriental.manage.controller.institution;

import com.oriental.manage.core.paging.Pagination;
import com.oriental.manage.core.response.ResponseDTO;
import com.oriental.manage.core.system.log.OperateLogger;
import com.oriental.manage.pojo.institution.ChannelRelationship;
import com.oriental.manage.service.institution.IChannelRelationshipService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * Created by dongyy on 2017/5/10.
 * 商户支付机构关系配置
 */
@Slf4j
@Controller
@RequestMapping("institution/channelRelationship")
public class ChannelRelationshipController {

    @Autowired
    private IChannelRelationshipService iChannelRelationshipService;

    @RequestMapping("init")
    public String init(){  return "institution/searchChannelRelationship";}

    @RequestMapping("toAdd")
    @RequiresPermissions("channelRelationship_add")
    public String toAdd(){ return "institution/addChannelRelationship";}

    @RequestMapping("toUpdate")
    @RequiresPermissions("channelRelationship_update")
    public String toUpdate(){ return "institution/updateChannelRelationship";}

    @OperateLogger(content = "查询商户支付机构关系配置",operationType = OperateLogger.OperationType.R)
    @RequestMapping("search")
    /*@RequiresPermissions("channelRelationship_select")*/
    public @ResponseBody
    ResponseDTO<Pagination<ChannelRelationship>> queryPage(Pagination<ChannelRelationship> pagination, ChannelRelationship channelRelationship){
        ResponseDTO<Pagination<ChannelRelationship>> responseDTO = new ResponseDTO<Pagination<ChannelRelationship>>();
        try {
            iChannelRelationshipService.queryPage(pagination,channelRelationship);
            responseDTO.setSuccess(true);
            responseDTO.setObject(pagination);
        } catch (Exception e) {
            log.error("查询失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg(e.getMessage());
        }
        return responseDTO;
    }

    @OperateLogger(content = "新增商户支付机构关系配置",operationType = OperateLogger.OperationType.C,tables = "T_CHANNEL_RELATIONSHIP")
    @RequestMapping("add")
    @RequiresPermissions("channelRelationship_add")
    @ResponseBody
    public ResponseDTO<String> add(ChannelRelationship channelRelationship){
        ResponseDTO<String> responseDTO = new ResponseDTO<String>();
        try{
            if(iChannelRelationshipService.check(channelRelationship)>0){
                responseDTO.setSuccess(false);
                responseDTO.setMsg("已经配置过支付机构关系");
            }else{
                channelRelationship.setCreateTime(new Date());
                channelRelationship.setLastUpdTime(new Date());
                iChannelRelationshipService.addChannelRelationship(responseDTO,channelRelationship);
            }
        }catch(Exception e){
            log.error("新增失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg(e.getMessage());
        }
        return responseDTO;
    }

    @OperateLogger(content = "修改商户支付机构关系配置",operationType = OperateLogger.OperationType.U,tables = "T_BUSITYPE_ORG_RELATIONSHIP")
    @RequestMapping("update")
    @RequiresPermissions("channelRelationship_update")
    @ResponseBody
    public ResponseDTO<String> update(ChannelRelationship channelRelationship){
        ResponseDTO<String> responseDTO = new ResponseDTO<String>();
        try{
            iChannelRelationshipService.updateChannelRelationship(responseDTO,channelRelationship);
        }catch(Exception e){
            log.error("修改失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg(e.getMessage());
        }
        return  responseDTO;
    }

    @OperateLogger(content = "删除商户支付机构关系配置",operationType = OperateLogger.OperationType.D,tables = "T_BUSITYPE_ORG_RELATIONSHIP")
    @RequestMapping("delete")
    @RequiresPermissions("channelRelationship_delete")
    @ResponseBody
    public ResponseDTO<String> delete(ChannelRelationship channelRelationship){
        ResponseDTO<String> responseDTO = new ResponseDTO<String>();
        try{
            iChannelRelationshipService.deleteChannelRelationship(responseDTO,channelRelationship);
        }catch(Exception e){
            log.error("修改失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg(e.getMessage());
        }
        return  responseDTO;
    }



}
